'''
Created on 07/07/2010

@author: Mar
'''
import unittest

from SimpleIndirectionBlock import SimpleIndirectionBlock
from DoubleIndirectionBlock import DoubleIndirectionBlock


class DoubleIndirectionBlockTest(unittest.TestCase):


    def setUp(self):
        self.block = DoubleIndirectionBlock()
        self.simpleBlock1 = SimpleIndirectionBlock()
        self.simpleBlock2 = SimpleIndirectionBlock()
        self.simpleBlock3 = SimpleIndirectionBlock()
        self.simpleBlock4 = SimpleIndirectionBlock()
        self.simpleBlock1.addDataPointer(1000, 1)        
        self.simpleBlock1.addDataPointer(1001, 2)        
        self.simpleBlock1.addDataPointer(1002, 3)        
        self.simpleBlock1.addDataPointer(1003, 4)        
        self.simpleBlock2.addDataPointer(2000, 10)        
        self.simpleBlock2.addDataPointer(2001, 11)        
        self.simpleBlock2.addDataPointer(2002, 12)        
        self.simpleBlock2.addDataPointer(2003, 13)        
        self.simpleBlock3.addDataPointer(3000, 20)        
        self.simpleBlock3.addDataPointer(3001, 21)        
        self.simpleBlock3.addDataPointer(3002, 22)        
        self.simpleBlock3.addDataPointer(3003, 23)        
        self.simpleBlock4.addDataPointer(4000, 30)        
        self.simpleBlock4.addDataPointer(3001, 31)        
        self.block.addIndirectionBlock(self.simpleBlock1)
        self.block.addIndirectionBlock(self.simpleBlock2)
        self.block.addIndirectionBlock(self.simpleBlock3)
        #self.block.addDataPointer(60000, 80)
        
        
    def tearDown(self):
        self.block = None
        self.simpleBlock1 = None  
        self.simpleBlock2 = None  
        self.simpleBlock3 = None
        self.simpleBlock4 = None
          


    


    def testHasSpace(self):
        self.assertTrue(self.block.hasSpace())
        
    def testHasSpaceToABlock(self):
        self.assertTrue(self.block.hasSpaceToABlock())
    
    def testAddIndirectionBlock(self):
        self.assertTrue(self.block.getPointers().__contains__(self.simpleBlock1))
        self.assertTrue(self.block.getPointers().__contains__(self.simpleBlock2))
        self.assertTrue(self.block.getPointers().__contains__(self.simpleBlock3))
    
    def testAddDataPointer(self):
        self.block.addDataPointer(10000, 50)
        self.assertTrue(self.simpleBlock1.getDataBlockPointers().__contains__(10000))
        
    def testGetFreeSpace(self):
        x = self.block.getFreeSpace()
        self.assertEqual(x, self.simpleBlock1)
        
    def testAddSameBlock(self):
        self.block.addIndirectionBlock(self.simpleBlock3)
        self.assertTrue(self.block.getPointers().count(self.simpleBlock3), 1)
        
    def testRemoveIndirectionBlock(self):
        self.block.removeIndirectionBlock(self.simpleBlock4)
        self.assertTrue(not(self.block.getPointers().__contains__(self.simpleBlock4)))
    
    def testRemoveDataBlock(self):
        self.block.addDataPointer(1111, 90)
        self.block.removeDataPointer(1111)
        self.assertTrue(not(self.simpleBlock1.getDataBlockPointers().__contains__(1111)))
        self.assertTrue(not(self.block.contains(1111)))
        
    def testContains(self):
        self.block.addDataPointer(1234, 45)
        self.assertTrue(self.block.contains(1234))
    
    def testGetBlocks(self):
        self.list = self.block.getBlocks()
        self.assertEqual(self.list[0], 1000)
        self.assertEqual(self.list[1], 1001)
        self.assertEqual(self.list[2], 1002)
        self.assertEqual(self.list[3], 1003)
        self.assertEqual(self.list[4], 2000)
        self.assertEqual(self.list[5], 2001)
        self.assertEqual(self.list[6], 2002)
        self.assertEqual(self.list[7], 2003)
        self.assertEqual(self.list[8], 3000)
        self.assertEqual(self.list[9], 3001)
        self.assertEqual(self.list[10], 3002)
        self.assertEqual(self.list[11], 3003)
        
        
    
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
    
suite = unittest.TestLoader().loadTestsFromTestCase(DoubleIndirectionBlockTest)
unittest.TextTestRunner(verbosity=2).run(suite)